package com.google.code.netbuilder.dal.providersimpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.google.code.netbuilder.dal.entities.CableWire;
import com.google.code.netbuilder.dal.entities.WireVersion;
import com.google.code.netbuilder.dal.providers.IWireVersion;
import com.google.code.netbuilder.dal.services.JDBCServise;

public class WireVersionJDBC implements IWireVersion {

	@Override
	public void save(WireVersion entity) {
		String query = "INSERT INTO wire_version(id, wire_id, field_name, value, version)" +
				" VALUES (?,?,?,?,?);";
		Connection con = null;
		PreparedStatement pst = null;
		try
		{
			con = JDBCServise.getConnection();
			if (con == null) throw new SQLException("No Data Base Connection");
			pst = con.prepareStatement(query);
			pst.setInt(1, entity.getId());
			pst.setInt(2, entity.getWireId());
			pst.setString(3, entity.getFieldName());
			pst.setString(4, entity.getValue());
			pst.setInt(5, entity.getVersion());
			pst.executeUpdate();
		}
		catch(SQLException e)
		{
			throw new RuntimeException(e);
		}
		finally
		{
			try {
				if (con != null) con.close();
				if (pst != null) pst.close();
			} catch (SQLException e) {
				throw new RuntimeException(e);
				}
		}

	}

	@Override
	public void update(WireVersion entity) {
		String query = "UPDATE wire_version SET wire_id=?, field_name=?" +
				", value=?, version=?  WHERE id=?;";
		
		Connection con = null;
		PreparedStatement pst = null;
		try {
			con = JDBCServise.getConnection();
			if (con == null) throw new SQLException("No Data Base Connection");
			pst = con.prepareStatement(query);
			pst.setInt(5, entity.getId());
			pst.setInt(1, entity.getWireId());
			pst.setString(2, entity.getFieldName());
			pst.setString(3, entity.getValue());
			pst.setInt(4, entity.getVersion());
			pst.executeUpdate();		
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
		finally
		{
			try {
				if (con != null) con.close();
				if (pst != null) pst.close();
			} catch (SQLException e) {
				throw new RuntimeException(e);
				}
		}
	}

	@Override
	public void delete(WireVersion entity) {
		String query = "DELETE FROM wire_version WHERE id=?;";
		
		Connection con = null;
		PreparedStatement pst = null;
		try {
			con = JDBCServise.getConnection();
			if (con == null) throw new SQLException("No Data Base Connection");
			pst = con.prepareStatement(query);
			pst.setInt(1, entity.getId());	
			pst.executeUpdate();	
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}		
		finally
		{
			try {
				if (con != null) con.close();
				if (pst != null) pst.close();
			} catch (SQLException e) {
				throw new RuntimeException(e);
				}
		}
	}

	@Override
	public List<WireVersion> findAll() {
		return getListWireVersion("select * from wire_version;");		
	}

	@Override
	public WireVersion findByID(Integer id) {
		Connection con = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		WireVersion w = null;
		
		try {
			con = JDBCServise.getConnection();
        
			pst = con.prepareStatement("Select * From wire_version where id="+id.toString());
			rs = pst.executeQuery();
        
			if(!rs.first()) throw new SQLException("Cable not found");;
						
			w= new WireVersion();
			w.setId(rs.getInt("id"));
			w.setFieldName(rs.getString("field_name"));
			w.setValue(rs.getString("value"));
			w.setWireId(rs.getInt("wire_id"));
			w.setVersion(rs.getInt("version"));			
			
		} catch (SQLException exc)
		{
			throw new RuntimeException(exc);
		}
		finally
		{
			try {
				if (con != null) con.close();
				if (pst != null) pst.close();
				if (rs != null) rs.close();
			} catch (SQLException exc) {
				throw new RuntimeException(exc);
				}
		}	
		
        return w;
	}

	@Override
	public List<WireVersion> getWireVersionsByVersion(Integer version) {		
		return getListWireVersion("Select * from wire_version where version="+version.toString()+";");
	}
	
	private List<WireVersion> getListWireVersion(String query)
	{
		Connection con = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		List<WireVersion> wv = null;
		
		try {
			con = JDBCServise.getConnection();
        
			pst = con.prepareStatement(query);
			rs = pst.executeQuery();
        
			wv = new ArrayList<WireVersion>();
        
			while (rs.next()) {
				WireVersion w= new WireVersion();
				w.setId(rs.getInt("id"));
				w.setFieldName(rs.getString("field_name"));
				w.setValue(rs.getString("value"));
				w.setWireId(rs.getInt("wire_id"));
				w.setVersion(rs.getInt("version"));
				wv.add(w);
			}
		} catch (SQLException exc)
		{
			throw new RuntimeException(exc);
		}
		finally
		{
			try {
				if (con != null) con.close();
				if (pst != null) pst.close();
				if (rs != null) rs.close();
			} catch (SQLException exc) {
				throw new RuntimeException(exc);
				}
		}	
		
        return wv;
	}

}
